package cate.game.role;

import cate.common.table.ins.InstanceRsRow;
import cate.game.GameBody;
import cate.game.util.GameKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.ToString;

@NoteClass(value = "主角的片段快照")
@ToString(callSuper = true)
public class RoleSnapshot {
    @NoteField(value = "主角的唯一ID")
    public String uid;

    @NoteField(value = "个人信息")
    public RoleSimpleBase base;

    @NoteField(value = "最近活跃时间")
    public long activeTime;

    @NoteField(value = "角色创建时间")
    public long createTime;

    @NoteField(value = "累计登录天数")
    public int loginDays;

    @NoteField(value = "是否在线")
    public boolean online;

    @NoteField(value = "是否是虚幻的（机器人）")
    public boolean unreal = false;

    @NoteField("历史最高共享星数")
    public int shareStarMax;

    public RoleSnapshot() {
        this.uid = GameKit.uid();
    }

    public RoleSnapshot(Role role) {
        this.read(role);
    }

    public RoleSnapshot read(Role role) {
        if (role == null) {
            return this;
        }
        this.uid = role.getUid();
        this.base = new RoleSimpleBase().read(role.getBase(false).copy());
        this.activeTime = role.getStatus() != null ? role.getStatus().activeTime : -1;
        this.createTime = role.getStatus() != null ? role.getStatus().createTime : -1;
        this.loginDays = role.getStatus() != null ? role.getStatus().loginDays : 0;
        this.online = role.getStatus() != null && role.getStatus().isOnline();
        this.shareStarMax = role.getRes().worldTree.heroShare.shareStarMax;
        return this;
    }

//    public RoleSnapshot read(RoleBase roleBase) {
//        if (roleBase == null) {
//            return this;
//        }
//        this.base = roleBase;
//        return this;
//    }

    public RoleSnapshot readByInstanceRow(InstanceRsRow row) {
        this.base = new RoleSimpleBase();
        if (row != null) {
            this.base.readByInstanceRow(row);
        }
        this.unreal = true;
        return this;
    }

    public RoleSnapshot initialize() {
        if (this.base == null) {
            this.base = new RoleSimpleBase();
        }
        return this;
    }

    /**
     * 利用本服活跃数据同步
     */
    public RoleSnapshot sync(GameBody game) {
        Role fm = game.role.getRoleFromActive(this.uid);
        if (fm != null) {
            this.online = true;
            read(fm);
        } else {
            this.online = false;
        }
        return this;
    }

    public RoleSnapshot copy() {
        RoleSnapshot cp = new RoleSnapshot();
        cp.uid = this.uid;
        cp.base = this.base == null ? null : this.base.copy();
        cp.activeTime = this.activeTime;
        cp.createTime = this.createTime;
        cp.loginDays = this.loginDays;
        cp.online = this.online;
        cp.unreal = this.unreal;
        return cp;
    }
}
